package cn.edu.zjgsu.ito.controller;

import cn.edu.zjgsu.ito.interceptor.AdminControllerInterceptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.List;

import cn.edu.zjgsu.ito.model.Classroom;
import cn.edu.zjgsu.ito.service.AdminLocManageService;
import cn.edu.zjgsu.ito.service.ServiceFactory;
import com.jfinal.aop.Before;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;


@Before(AdminControllerInterceptor.class)
public class AdminLocManageController extends Controller {

	AdminLocManageService adminLocManageService=ServiceFactory.createAdminLocManageService();

	/**
	 * 教室管理初始化
	 * 黄凯航
	 */
	public void index()
	{
		List<Record> classrooms=Db.find("select * from classroom");

		List<Record> classroomTypes=Db.find("select classroomType from classroom");
		HashSet<Record> c=new HashSet<Record>(classroomTypes);
		classroomTypes.clear();
		classroomTypes.addAll(c);

		setAttr("classroomType",classroomTypes);
		setAttr("classroom",classrooms);
		render("locationmanage.html");
	}
	/**
	 * 添加教室跳转页面
	 * 黄凯航
	 */
	public void add()
	{
		render("addlocation.html");
	}
	/**
	 * 添加教室
	 * 黄凯航
	 */
	public void submit()
	{		
		Classroom classroom=getModel(Classroom.class,"classroom");		
		String error=adminLocManageService.addSingleClassroom(classroom);
		setAttr("error",error);
		index();

	}
	/**
	 * 编辑教室跳转页面
	 * 黄凯航
	 */
	public void edit()
	{
		Integer id=getParaToInt(0);
		if(id!=null&&id>0)
			setAttr("classroom",Classroom.dao.findById(id));
		render("correctlocation.html");
	}
	/**
	 * 教室更新
	 * 黄凯航
	 */
	public void update()
	{
		System.out.println("come");	
		Classroom classroom=getModel(Classroom.class,"classroom");
		System.out.println("!"+classroom);
		String error=adminLocManageService.updateSingleClassroom(classroom);
		setAttr("error",error);
		index();	
	}
	/**
	 * 异步教室更新
	 * 黄凯航
	 */
	public void ajaxupdate()
	{
		int classroomVolume=getParaToInt("classroomVolume");
		int examVolume=getParaToInt("examVolume");
		String classroomType=getPara("classroomType");
		int id=getParaToInt("id");

		Classroom classroom = getModel(Classroom.class);
		classroom.set("classroomVolume", classroomVolume);
		classroom.set("examVolume", examVolume);
		classroom.set("classroomType", classroomType);
		classroom.set("id", id);

		String error=adminLocManageService.updateSingleClassroom(classroom);

		setAttr("classroom",Classroom.dao.find("select * from classroom where id=?",id));
		setAttr("error",error);
		renderJson();


	}
	/**
	 * 教室查找
	 * 黄凯航
	 */
	public void search()
	{ 
		String keyword=getPara("keyword");
		if(keyword.equals("all"))
			index();
		else
		{
			List<Record> classrooms=Db.find("select * from classroom where classroomType=?",keyword);
			setAttr("classroom",classrooms);

			List<Record> classroomTypes=Db.find("select classroomType from classroom");
			HashSet<Record> c=new HashSet<Record>(classroomTypes);
			classroomTypes.clear();
			classroomTypes.addAll(c);

			setAttr("classroomType",classroomTypes);
			render("locationmanage.html");
		}
	}
	/**
	 * 异步教室查找
	 * 黄凯航
	 */
	public void ajaxsearch()
	{
		String keyword=getPara("keyword");

		if(keyword.equals("all"))
		{
			List<Classroom> classroom=Classroom.dao.find("select * from classroom");
			renderJson(classroom);
		}

		else
		{
			List<Classroom> classroom=Classroom.dao.find("select * from classroom where classroomType=?",keyword);
			renderJson(classroom);
		}
	}

	/**
	 * 批量删除方法
	 * 黄凯航
	 */
	public void batch()
	{
		try{
			int id=0;
			//String[] locCheckboxs=getParaValues("locCheckbox");
			Record locationClassroomNo=new Record();
			String classroomNo="";
			String checkboxValue=getPara("checkboxValue");
			String[] locCheckboxs=checkboxValue.split(",");
			for(int i=0;i<locCheckboxs.length;i++)
			    System.out.print(i+"="+locCheckboxs[i]+"  ");
			for(int i=0;i<locCheckboxs.length;i++)
			{
				id=Integer.parseInt(locCheckboxs[i]);
				locationClassroomNo=Db.findById("classroom", id);
				classroomNo=locationClassroomNo.get("classroomNo");
				adminLocManageService.deleteOrNot(classroomNo);
				locationClassroomNo=new Record();
			}
			redirect("/adminlocmanage");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redirect("/adminlocmanage");
		}
	}
	/**
	 * 教室模板下载方法
	 * 黄凯航
	 */
	public void fileDownload()
	{
		String fileName="教室模板.xls";
		String path=JFinal.me().getConstants().getFileRenderPath();
		path=path+"/"+fileName;
		renderFile(new File(path));
	}
	/**
	 * 教室模板上传方法
	 * 黄凯航
	 */
	public void fileUpload()
	{
		String filedir=PathKit.getWebRootPath();
		filedir=filedir+"\\WEB-INF\\upload";
		try
		{
			UploadFile file=getFile("uploadFile");
			String fileName=file.getFileName();
			String extension=fileName.substring(fileName.lastIndexOf("."));
			if(extension.equals(".xls")||extension.equals(".XLS"))
			{
				File source=file.getFile();
				String newFileName=System.currentTimeMillis()+extension;
				FileInputStream fis=new FileInputStream(source);
				File targetDir=new File(filedir);
				File target=new File(targetDir,newFileName);
				FileOutputStream fos=new FileOutputStream(target);
				byte[] bts=new byte[300];
				while (fis.read(bts,0,300)!=-1){
					fos.write(bts,0,300);
				}
				fos.close();
				fis.close();
				source.delete();
				String error=adminLocManageService.readLocationTemplate(fileName, newFileName, filedir);
				setAttr("error",error);
				index();
			}
			else
			{
				setAttr("error","文件类型错误");
				index();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
